package com.cby.springboot.demo.controller;

import com.cby.springboot.demo.bean.Book;
import com.cby.springboot.demo.bean.Person;
import com.cby.springboot.demo.bean.User;
import com.cby.springboot.demo.exception.UserNotExistException;
import com.cby.springboot.demo.service.PersonService;
import com.cby.springboot.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class LoginController extends BaseController{


    @RequestMapping("/userlist")
    public String index(Model model) {
        List<User> users = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setId(i);
            user.setUserName("jason:"+i);
            user.setEmail("厦门:"+i);
            users.add(user);
        }
        model.addAttribute("users",users);
        return "userlist";
    }

    @Autowired
    PersonService service;

    /**
     * 跳转到登陆页面
     *
     * @return
     */
    @GetMapping(value = {"/login"})
    public String loginPage() {
        //跳转到登录页 login.html
        Person p = service.getPersonInfo();
        logI(p.getLastName());
        return "login";
    }



    @Autowired
    UserService userService;

    @RequestMapping(value = "/search/user")
    @ResponseBody
    public User getByName(String userName) {
        logI("getByName");
        return userService.getByName(userName);
    }


    @RequestMapping("/doLogin")
    public String login(User user, Map<String, Object> map, HttpSession session) {
        if(user != null) {
            logI(user.toString());
        }
        //从数据库中查询用户信息
        User loginUser = userService.getByUserNameAndPassword(user);
        if (loginUser != null) {
            session.setAttribute("loginUser", loginUser);
            //防止重复提交使用重定向
            return "redirect:/home.html";
        } else {
            map.put("msg", "用户名或密码错误");
            logI("登陆失败");
            return "login";
        }
    }

    @RequestMapping("/doRegister")
    public String register(User user, String rePassword, Map<String, Object> map, HttpSession session) {
        logI("register, rePassword = "+rePassword);
        if(user != null) {
            user.setUserId(UUID.randomUUID().toString());
            logI(user.toString());
        }

        if(user != null && !StringUtils.hasText(user.getEmail())){
            map.put("msg", "邮箱不能为空");
            logI("注册失败");
            return "registration";
        }

        if(user != null && !StringUtils.hasText(user.getUserName())){
            map.put("msg", "用户名不能为空");
            logI("注册失败");
            return "registration";
        }

        if(StringUtils.hasText(rePassword)
                && user != null
                && !rePassword.equals(user.getPassword())) {
            map.put("msg", "密码不一致");
            logI("注册失败");
            return "registration";
        }



        //从数据库中查询用户信息
        User loginUser = userService.getByUserName(user.getUserName());
        if(loginUser != null) {
            map.put("msg", "用户已存在");
            logI("注册失败");
            return "registration";
        }

        //从数据库中查询用户信息
        int result = userService.insert(user);
        if (result > 0) {
            //防止重复提交使用重定向
            return "redirect:/login.html";
        } else {
            map.put("msg", "请检查填写的信息内容");
            logI("注册失败");
            return "registration";
        }
    }

//    @RequestMapping("/doLogin")
//    public ModelAndView login(HttpServletRequest request) {
//        String userName = request.getParameter("username");
//        String password = request.getParameter("password");
//        logI("userName = "+userName+", password = "+password);
//        //跳转到index.html
//        ModelAndView mav = new ModelAndView("/index");
//        return mav;
//    }

//    @RequestMapping("/doLogin")
//    public ModelAndView login(HttpServletRequest request) {
//        String userName = request.getParameter("username");
//        String password = request.getParameter("password");
//        logI("userName = "+userName+", password = "+password);
//        //跳转到home.index
//        ModelAndView mav = new ModelAndView("/home");
//        //传参数name
//        mav.addObject("name", userName);
//        return mav;
//    }

//    @RequestMapping("/hello")
//    public String hello(Map<String, Object> map) {
//        //通过 map 向前台页面传递数据
//        map.put("name", "编程帮（www.biancheng.net）");
//        return "hello";
//    }


    @GetMapping(value = {"/testException"})
    public String testException(String user) {
        if ("user".equals(user)) {
            throw new UserNotExistException();
        }
        //跳转到登录页 login.html
        return "login";
    }
}
